package git.soulbgm;

import git.soulbgm.utils.RocksDBUtil;
import org.rocksdb.RocksDBException;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 程序入口
 *
 * @author SoulBGM
 * @date 2022/01/26
 */
public class Application {

    public static void main(String[] args) throws RocksDBException {
        // 添加或修改
        RocksDBUtil.put("ak", "a1", "aq1");
        RocksDBUtil.put("ak", "a2", "aq2");
        RocksDBUtil.put("ak", "a3", "aq3");
        RocksDBUtil.put("ak", "a4", "aq4");

        Map<String, String> acMap = new HashMap<>(5);
        acMap.put("b1", "bq1");
        acMap.put("b2", "bq2");
        acMap.put("b3", "bq3");
        acMap.put("b4", "bq4");
        acMap.put("b5", "bq5");
        RocksDBUtil.batchPut("ac", acMap);

        // 查询
        System.out.println(new String(RocksDBUtil.get("ak", "a1")));
        RocksDBUtil.put("ak", "a1", "update-aq1");
        System.out.println(new String(RocksDBUtil.get("ak", "a1")));
        System.out.println(new String(RocksDBUtil.get("ak", "a2")));
        System.out.println(new String(RocksDBUtil.get("ak", "a3")));
        System.out.println(new String(RocksDBUtil.get("ak", "a4")));

        print(RocksDBUtil.multiGetAsMap("ac", new String[]{"b1", "b2", "b3"}));
        print(RocksDBUtil.getAll("ac"));

        // 删除
        RocksDBUtil.delete("ck", "b4");
    }

    private static void print(Map<String, byte[]> map) {
        Set<String> keySet = map.keySet();
        keySet.forEach(k -> System.out.printf("k:%s v:%s\n", k, new String(map.get(k))));
    }

}
